//
//  QDShareManager.swift
//  QDMetroClient
//
//  Created by YueWen on 2019/3/21.
//  Copyright © 2019 YueWen. All rights reserved.
//

import UIKit


/// 分享管理
final class QDShareManager: NSObject {

    /// 单例
    static let shareInstance = QDShareManager()
    
    private override init() {
        super.init()
        
        //初始化
        UMSocialManager.default()?.setPlaform(.wechatSession, appKey: WX_APP_KEY, appSecret: WX_APP_SECRET, redirectURL: "https://mobile.umeng.com/social")
        
        UMSocialManager.default()?.setPlaform(.wechatTimeLine, appKey: WX_APP_KEY, appSecret: WX_APP_SECRET, redirectURL: "https://mobile.umeng.com/social")
        
        UMSocialManager.default()?.setPlaform(.QQ, appKey: QQ_APP_KEY, appSecret: nil, redirectURL: "https://mobile.umeng.com/social")
        
        UMSocialManager.default()?.setPlaform(.sina, appKey: SINA_APP_KEY, appSecret: SINA_APP_SECRET, redirectURL: "https://sns.whalecloud.com/sina2/callback")
        
        //分享的不必须是https
        UMSocialGlobal.shareInstance()?.isUsingHttpsWhenShareContent = false
    }
    
    
    /// 进行分享
    ///
    /// - Parameters:
    ///   - url: 分享的url
    ///   - imageUrl: 分享的图片url
    ///   - title: 分享的标题
    ///   - desc: 分享的描述
    ///   - from: 来自的控制器
    @objc class func share(url: String,
                           imageUrl: String?,
                           title: String,
                           desc: String? = "",
                           from: UIViewController?,
                           success: (()->())? = nil,
                           fail:(()->())? = nil){
        
        shareInstance.share(url: url, imageUrl: imageUrl, title: title, desc: desc, from: from, success: success, fail: fail)
    }
    

    
    private func share(url: String,
                     imageUrl: String?,
                     title: String,
                     desc: String? = "",
                     from: UIViewController?,
                     success: (()->())? = nil,
                     fail:(()->())? = nil){
        
        QDSocialPlatformConfig.configSocialPlatform()
        weak var formViewController = from
        
        UMSocialUIManager.showShareMenuViewInWindow { (type, userInfo) in
            
            //进行数据拼接
            let shareObject = UMShareWebpageObject.shareObject(withTitle: title, descr: desc ?? "", thumImage: imageUrl ?? "metro_share".namedImage)
            shareObject?.webpageUrl = url
            
            //信息载体
            let messageObject = UMSocialMessageObject()
            messageObject.shareObject = shareObject
            
            //分享
            UMSocialManager.default()?.share(to: type, messageObject: messageObject, currentViewController: formViewController, completion: { (data, error) in
                
                guard error == nil else {
                    fail?()
                    formViewController?.view.showTextHUD(text: "分享失败!"); return
                }
                
                success?()
                if let shareData = data as? UMSocialShareResponse, let message = shareData.message {
                    if !message.isEmpty {
                        formViewController?.view.showTextHUD(text: "分享成功!")
                    }
                }
            })
        }
    }
}
